package edu.gatech.cc.CFAWaffleFriesWheresMyStuff.gui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.EditText;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.R;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Datastore;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.Datastore.UserDoesNotExistException;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.DatastoreFactory;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.UserManager;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.UserManager.IncorrectPasswordException;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.UserManager.LoginAttemptLimitExceededException;
import edu.gatech.cc.CFAWaffleFriesWheresMyStuff.common.User;

/**
 * Displays login form for user and passes appropriate information to Login instance.
 * Transitions to either homescreen or registration activity.
 * @author Matthew Barulic
 *
 */
public class LoginActivity extends Activity {

	private UserManager _loginManager;
	private User _loggedInUser;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		Datastore datastore = DatastoreFactory.getDatastore(getApplicationContext());
		
		_loginManager = new UserManager(datastore);
		
		final LoginActivity me = this;
		
		((TextView)findViewById(R.id.loginSignInButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String username = ((EditText)findViewById(R.id.loginUsernameText)).getText().toString();
				String password = ((EditText)findViewById(R.id.loginPasswordText)).getText().toString();
				me.login(username, password);
			}
		});
		
		((TextView)findViewById(R.id.loginRegisterButton)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				me.showRegistration();
			}
		});
		
		
	}
	
	/**
	 * Passes the given user information on to an instance of Login.
	 * If the login attempt succeeds, this method will launch the
	 * home screen activity and close this activity.
	 * @param username
	 * @param password
	 */
	private void login(String username, String password)
	{
		try {
			_loggedInUser = _loginManager.attemptLogin(username, password);
			// If login failed, an exception would be thrown.
			// If the following code runs, the login succeeded.
			Intent i = new Intent(getApplicationContext(), HomeScreenActivity.class);
			i.putExtra("user", _loggedInUser.getUsername());
			startActivity(i);
			finish();
			
		} catch (IncorrectPasswordException e) {
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
			alertDialogBuilder.setTitle("Sign In Failed");
			alertDialogBuilder.setMessage("Incorrect Password");
			alertDialogBuilder.setCancelable(false);
			alertDialogBuilder.setNeutralButton("OK", null);
			AlertDialog alert = alertDialogBuilder.create();
			alert.show();
		} catch (UserDoesNotExistException e) {
			final LoginActivity me = this;
			AlertDialog alert = new AlertDialog.Builder(this).create();
			alert.setTitle("Sign In Failed");
			alert.setMessage("No such user exists. Would you like to register a new account?");
			alert.setCancelable(false);
			alert.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent i = new Intent(getApplicationContext(), RegistrationActivity.class);
					me.startActivity(i);
					finish();
				}
			});
			alert.setButton(DialogInterface.BUTTON_NEGATIVE,  "No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// Do nothing
				}
			});
			alert.show();
		} catch (LoginAttemptLimitExceededException e) {
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
			alertDialogBuilder.setIcon(R.drawable.lock);
			alertDialogBuilder.setTitle("Sign In Failed");
			alertDialogBuilder.setMessage("Your account is locked.\nPlease contact an admin to unlock it for you.");
			alertDialogBuilder.setCancelable(false);
			alertDialogBuilder.setNeutralButton("OK", null);
			AlertDialog alert = alertDialogBuilder.create();
			alert.show();
		}
	}
	
	/**
	 * Starts RegistrationActivity setup to make a new User
	 * Finishes this acativity 
	 */
	private void showRegistration()
	{
		Intent i = new Intent(getApplicationContext(), RegistrationActivity.class);
		i.putExtra("isAdmin", false);
		startActivity(i);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}
	

}
